from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QLabel, QWidget, QDoubleSpinBox, QComboBox, QFormLayout, QApplication


class Form(QWidget):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        principleLabel = QLabel("Principle: ")
        self.principleSpinBox = QDoubleSpinBox()
        self.principleSpinBox.setRange(1.00, 999999999999999.00)
        self.principleSpinBox.setValue(1.00)
        self.principleSpinBox.setPrefix("$")

        rateLable = QLabel("Rate: ")
        self.rateSpinBox = QDoubleSpinBox()
        self.rateSpinBox.setRange(0.00, 100)
        self.rateSpinBox.setValue(3)
        self.rateSpinBox.setSuffix("%")

        yearLabel = QLabel("Years: ")
        self.yearComboBox = QComboBox()
        years = ["一年期", "二年期", "三年期", "四年期", "五年期"]
        self.yearComboBox.addItems(years)

        amountLabel = QLabel("Amount: ")
        self.amountResult = QLabel("0.00")

        layout = QFormLayout()
        layout.addRow(principleLabel, self.principleSpinBox)
        layout.addRow(rateLable, self.rateSpinBox)
        layout.addRow(yearLabel, self.yearComboBox)
        layout.addRow(amountLabel, self.amountResult)

        self.setLayout(layout)
        self.updateUI()

        self.principleSpinBox.valueChanged.connect(self.updateUI)
        self.rateSpinBox.valueChanged.connect(self.updateUI)
        self.yearComboBox.currentIndexChanged.connect(self.updateUI)

    def updateUI(self):
        principal = self.principleSpinBox.value()
        rate = self.rateSpinBox.value()
        years = self.yearComboBox.currentIndex() + 1
        amount = principal * ((1 + (rate / 100.0)) ** years)
        self.amountResult.setText("{:,.2f}".format(amount))


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec())
